
*Set root data directory
local rootdir
cd "`rootdir'"



**Generate food retail employment share and pop.
foreach round in 38 66{
use "`rootdir'round`round'_sch10food", clear

merge m:m region round using mult`round', keepusing(regioncode)
drop _merge

bysort regioncode sector: egen nsspop=total(mult)
bysort regioncode sector: egen foodwrk=total(food*mult)
gen foodshare=foodwrk/nsspop

bysort regioncode sector: egen foodshare_uw=mean(food)

duplicates drop regioncode sector, force
keep round regioncode nsspop* foodshare* sector
save "`rootdir'nss_reg_foodpop_`round'", replace
}


**Generate pce and dispersion measures for all regions...
foreach round in 38 66{
use round`round'_edit, clear
merge 1:1 sector subround region subsample segment substratum fsu hhno using round`round'_hh, keepusing(mpce mult)
drop _merge
merge m:m region round using mult`round', keepusing(regioncode)
drop _merge

gen state=floor(region/10)
gen statecode=state

****generate "presence" measures (and district sample hh count and district sample mean exp.)
gen rnumfood=0
gen rsnumfood=0


foreach i in grains veg{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
bysort regioncode sector: egen meduv`i'_`j'=median(x`i'_`j'/q`i'_`j')
}
}

*dispersion in exp. shares
foreach i in grains veg{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
gen share`i'_`j'=x`i'_`j'/x`i'
replace share`i'_`j'=. if share`i'_`j'==1 | share`i'_`j'==0
}
}

aorder

*dispersion in unit values
foreach j in share meduv{
egen `j'grains_sd=rowsd(`j'grains_1-`j'grains_18)
egen `j'grains_mean=rowmean(`j'grains_1-`j'grains_18)
gen `j'grains_cv=`j'grains_sd/`j'grains_mean

egen `j'veg_sd=rowsd(`j'veg_1-`j'veg_29)
egen `j'veg_mean=rowmean(`j'veg_1-`j'veg_29)
gen `j'veg_cv=`j'veg_sd/`j'veg_mean
}

*average or median of the "slope"?
foreach j in cv{
bysort regioncode sector: egen medsharegrains_`j'=median(sharegrains_`j')
*bysort regioncode: egen meansharegrains_`j'=mean(share_`j')
bysort regioncode sector: egen medshareveg_`j'=median(shareveg_`j')
*bysort regioncode: egen meanshareveg_`j'=mean(share_`j')
}



foreach i in grains veg{
gen vnum`i'=0
gen rnum`i'=0
levelsof n`i'tot, local(nmax)
forvalues j=1(1)`nmax'{ 
bysort fsu: egen zmax=max(x`i'_`j')
replace vnum`i'=vnum`i'+1 if zmax>0
drop zmax 

bysort regioncode sector: egen zmax=max(x`i'_`j')
replace rnum`i'=rnum`i'+1 if zmax>0
drop zmax
}

bysort regioncode sector: egen rvmeannum`i'=mean(vnum`i')
bysort regioncode sector: egen rvmediannum`i'=median(vnum`i')

}



bysort regioncode sector: egen meanpce=mean(mpce)
bysort regioncode sector: egen medpce=median(mpce)

gen lnmeanpce=ln(meanpce)
gen lnmedpce=ln(medpce)

duplicates drop regioncode sector, force
keep regioncode sector round lnmeanpce lnmedpce medshare* meduv*cv r*num* 
save "`rootdir'nss_reg_disppce_`round'", replace
}


**Generate region level roads (and land area for pop. density?)

cd "`rootdir'"

use "`rootdir'round66_sch10hh", clear
sort sector region fsu
bysort fsu sector: egen minddist=min(districtcode)
bysort fsu sector: egen maxddist=max(districtcode)
replace districtcode=minddist if districtcode==.
replace districtcode=maxddist if districtcode==0 & maxddist~=.

gen statecode=floor(region/10)
tab statecode

***
*ren regioncode region
merge m:m region round using "`rootdir'mult66", keepusing(regioncode)
drop _merge region


duplicates drop statecode districtcode, force

merge m:m statecode districtcode using nss66_nm61
drop _merge
merge m:m statename districtname using district_poparea2011
drop _merge

ren statename statename66
ren districtname districtname66
merge m:m statename66 districtname66 using district_conversion
drop if statenamedist==""
drop if districtnamedist==""
bysort statenamedist districtnamedist: egen totarea=total(areasqkm)
bysort statenamedist districtnamedist: egen totpop=total(pop)
duplicates drop statenamedist districtnamedist, force
keep statenamedist districtnamedist regioncode totarea totpop
merge 1:m statenamedist districtnamedist using "`rootdir'temp", keepusing(lroad* year *mrkt* pop_t totgeog)

*bysort year: summ lroad2

bysort year regioncode: egen rarea1=total(totarea)
bysort year regioncode: egen rarea2=total(totgeog)

gen pop1=totpop
gen pop2=pop_t



summ pop* rarea* lroad* if year==1983
summ pop* rarea* lroad* if year==2009



foreach j in lroad lroad2 lroad3 ppmrkt psmrkt ptmrkt pop1 pop2{
bysort year regioncode: egen r`j'=total(`j')
gen r`j'sqkm=r`j'/rarea1
}

*keep if year==1983 | year==2009
gen round=38 if year==1983
replace round=66 if year==2009
keep if round~=.
drop _merge
duplicates drop round regioncode, force
**need to convert to right regioncodes

summ pop* rarea* lroad* if round==38
summ pop* rarea* lroad* if round==66

save "`rootdir'nss_reg_road_area", replace






use "`rootdir'nss_reg_foodpop_38", clear
append using "`rootdir'nss_reg_foodpop_66"
merge 1:1 regioncode round sector using "`rootdir'nss_reg_disppce_38", update replace
drop _merge
merge 1:1 regioncode round sector using "`rootdir'nss_reg_disppce_66", update replace
drop _merge

merge m:1 regioncode round using "`rootdir'nss_reg_road_area", update replace
drop _merge

merge m:1 regioncode round sector using "`rootdir'parameters_merge", keepusing(F*_a n*_a eps*_a psi*_a) replace update
cap: drop _merge


bysort regioncode round sector: gen rcounter=_n
egen regsector=group(regioncode sector)

cap: gen lnroad=ln(rlroad2sqkm)
cap: gen lnpop=ln(rpop1sqkm)

gen baseroad=rlroad2sqkm if round==38
bysort regioncode: egen road38=min(baseroad)
gen lnroad38=ln(road38)

summ foodshare_uw lnroad38 rlroad2sqkm lnpop lnmeanpce  if rcounter==1 & round==66


cap: gen lnnsspop=ln(nsspop)
cap: gen lnfoodshare=ln(foodshare)
*bysort round: summ foodshare_uw lnnsspop lnmeanpce

bysort sector: summ n*_p50_a
cap: gen ngrainsfix=4.9
cap: gen nvegfix=13.6



gen state=floor(regioncode/10)





*****Table A.14: correlation of variety cost estimates with proxies for retail environment


local round 66
local varlist lnmeanpce lnnsspop 

local i grains


cap: gen lnpredictF`i'=ln(F`i'_a*(n`i'_p50_a^eps`i'_a))
cap: gen lnpredictF`i'_alt=ln(F`i'_a*(n`i'fix^eps`i'_a))
reg lnpredictF`i'_alt lnfoodshare if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') replace ctitle(`i' n-fix)
reg lnpredictF`i'_alt lnfoodshare `varlist' if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') append ctitle(`i' n-fix)

reg lnpredictF`i' lnfoodshare if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') append ctitle(`i')
reg lnpredictF`i' lnfoodshare `varlist' if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') append ctitle(`i')

local i veg

cap: gen lnpredictF`i'=ln(F`i'_a*(n`i'_p50_a^eps`i'_a))
cap: gen lnpredictF`i'_alt=ln(F`i'_a*(n`i'fix^eps`i'_a))
reg lnpredictF`i'_alt lnfoodshare if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') append ctitle(`i' n-fix)
reg lnpredictF`i'_alt lnfoodshare `varlist' if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') append ctitle(`i' n-fix)

reg lnpredictF`i' lnfoodshare if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') append ctitle(`i')
reg lnpredictF`i' lnfoodshare `varlist' if rcounter==1 & round==`round', vce(cluster regioncode)
outreg2 using param.tex, dec(3) keep(lnfoodshare `varlist') append ctitle(`i')


